var utilsmd5 = require('../../utils/md5.js')
var app = getApp();
Page({
  data: {
    arraydetail: [],
    orderId: 0,
    author: 'Lotus'
  },

  buy: function () {
    var that = this;
    console.log("buy start");
    var service = 'Order.payForOrder';
    var orderId = that.data.orderId;
    var otherType = 'wxcxjsapipay';
    var otherFee = that.data.arraydetail.totalFee
    console.log("openId:" + app.globalData.openId);
    console.log("orderId:" + orderId);
    console.log("otherType:" + otherType);
    console.log("otherFee:" + otherFee);
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId, otherType: otherType, otherFee: otherFee },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {

        console.log("success:" + res.data.data);

        if (!res.data.data.code) {
          var info = res.data.data.info.tradeNo;
          var param = [];
          param['appid'] = info.appId;
          param['timeStamp'] = info.timeStamp;
          param['nonceStr'] = info.nonceStr;
          param['package'] = info.package;
          param['paySign'] = info.sign;

          // param['prepayid'] = info.prepayid; 
          // param['partnerid'] = info.partnerid;
          that.pay(param);
        } else {
          wx.showToast({
            title: res.data.data.message,
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.log("fail:" + res);
      },
    })
    console.log("buy end");
  },

  pay: function (param) {
    var that = this;
    console.log("支付 start");
    console.log(param)
    var appid = app.globalData.APPID;
    var timeStamp = '' + param.timeStamp;
    var nonceStr = param.nonceStr;
    var pkg = param.package;
    var signType = 'MD5';
    //var key = app.globalData.KEY;
    //var paySign = utilsmd5.hexMD5('appId=' + appid + '&nonceStr=' + nonceStr + '&package=' + pkg + '&signType=' + signType + '&timeStamp=' + timeStamp + '&key=' + key).toUpperCase();
    var paySign = param.paySign;
    wx.requestPayment({
      timeStamp: timeStamp,
      nonceStr: nonceStr,
      package: pkg,
      signType: signType,
      paySign: paySign,
      success: function (res) {
        // success
        console.log("支付")
        //console.log(res)
        wx.redirectTo({
          url: 'orderlist'
        })
      },
      fail: function (res) {
        // fail
        console.log("支付失败")
        console.log(res)
      },
      complete: function () {
        // complete

        that.setData({
          buttonconfirm: true,
          buttonbuy: false
        })
        wx.redirectTo({
          url: 'orderlist'
        })
      }
    })
    console.log("支付 end");
  },
  shouhuo: function () {
    var that = this;
    var service = 'Order.receiveOrder';
    var orderId = that.data.orderId;
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        wx.showToast({
          title: res.data.data.message,
          icon: 'success',
          duration: 2000
        })
        wx.redirectTo({
          url: 'orderlist'
        })
      },
      fail: function (res) {
        //console.log("fail:" + res);
      },
    })
  },
  cancel: function () {
    var that = this;
    var service = 'Order.cancelOrder';
    var orderId = that.data.orderId;
    var canelReason = '';
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId, canelReason: canelReason },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        wx.showToast({
          title: res.data.data.message,
          icon: 'success',
          duration: 2000
        })
        wx.redirectTo({
          url: 'orderlist'
        })
      },
      fail: function (res) {
        //console.log("fail:" + res);
      },
    })
  },
  getlist: function () {
    var that = this;
    var service = 'Order.getOrderInfo';
    var orderId = that.data.orderId;
    wx.request({
      method: 'POST',
      url: app.globalData.APIURL,
      data: { service: service, openId: app.globalData.openId, orderId: orderId },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        //console.log("success:" + res.data.data);    
        if (!res.data.data.code) {
          that.setData({
            arraydetail: res.data.data.info
          })
        }
      },
      fail: function (res) {
        console.log("fail:" + res);
      },
    })

  },
  gotocomment: function () {
  var that = this;
    var orderId = that.data.orderId;
    wx.navigateTo({
      url: 'ordercomment?orderId='+orderId
    })

  },
  onLoad: function (options) {
    var that = this;
    if (options.fromInviteCode) {
      app.globalData.fromInviteCode=options.fromInviteCode;
    }
    app.appstart();
    if (options.orderId) {
      console.log("orderId:" + options.orderId);
      this.setData({ orderId: options.orderId });
    }
    this.getlist();
  }
})